package com.jxbeidou.system.log.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import com.jxbeidou.core.entity.PageResult;
import com.jxbeidou.core.mybatis.service.impl.BaseServiceImpl;
import com.jxbeidou.core.mybatis.convert.PageResultConvert;
import com.jxbeidou.system.log.convert.LogOperateConvert;
import com.jxbeidou.system.log.entity.LogOperateDO;
import com.jxbeidou.system.log.query.LogOperateQuery;
import com.jxbeidou.system.log.vo.LogOperateVO;
import com.jxbeidou.system.log.mapper.LogOperateMapper;
import com.jxbeidou.system.log.service.LogOperateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 操作日志表
 *
 * @author 杜晓祥 
 * @since 1.0.0 2023-12-14
 */
@Service
@AllArgsConstructor
public class LogOperateServiceImpl extends BaseServiceImpl<LogOperateMapper, LogOperateDO> implements LogOperateService {

    @Override
    public PageResult<LogOperateVO> page(LogOperateQuery query) {
        IPage<LogOperateDO> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        PageResult<LogOperateVO> result = PageResultConvert.convert(page, entityList -> LogOperateConvert.INSTANCE.convertList(entityList));
        return result;
    }

    private LambdaQueryWrapper<LogOperateDO> getWrapper(LogOperateQuery query){
        LambdaQueryWrapper<LogOperateDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StringUtils.isNotEmpty(query.getModule()), LogOperateDO::getModule, query.getModule());
        wrapper.eq(StringUtils.isNotEmpty(query.getName()), LogOperateDO::getName, query.getName());
        return wrapper;
    }

    @Override
    public void save(LogOperateVO vo) {
        LogOperateDO entity = LogOperateConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(LogOperateVO vo) {
        LogOperateDO entity = LogOperateConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}